package com.tts.dede;

import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Fruits extends Activity implements OnClickListener, OnInitListener {

	// Random declaration
	private Random RANDOM = new Random();

	// TTS object
	private TextToSpeech myTTS;
	// status check code
	private int MY_DATA_CHECK_CODE = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fruits);

		// Capture our button from layout
		Button button = (Button) findViewById(R.id.Back2);
		// Register the onClick listener with the implementation above
		button.setOnClickListener(this);
		((Button) findViewById(R.id.Back2))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(),
								TTSActivity.class);
						startActivity(intent);
						finish();
					}
				});

		// Capture our button from layout
		Button button1 = (Button) findViewById(R.id.New2);
		// Register the onClick listener with the implementation above
		button1.setOnClickListener(this);
		((Button) findViewById(R.id.New2))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(),
								Fruits.class);
						startActivity(intent);
						finish();
					}
				});

		((Button) findViewById(R.id.buttonApple)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonBanana)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonMelon)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonOrange)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonPeach)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonPear)).setVisibility(View.GONE);

		((Button) findViewById(R.id.buttonFraise)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonPineapple)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonLemon)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonGrappe)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonCerise)).setVisibility(View.GONE);
		((Button) findViewById(R.id.buttonKiwi)).setVisibility(View.GONE);

		int rand = RANDOM.nextInt(12);
		switch (rand) {
		case 0: {
			((Button) findViewById(R.id.buttonApple))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 1: {
			((Button) findViewById(R.id.buttonBanana))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 2: {
			((Button) findViewById(R.id.buttonMelon))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 3: {
			((Button) findViewById(R.id.buttonOrange))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 4: {
			((Button) findViewById(R.id.buttonPeach))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 5: {
			((Button) findViewById(R.id.buttonPear))
					.setVisibility(View.VISIBLE);
		}
			break;

		case 6: {
			((Button) findViewById(R.id.buttonFraise))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 7: {
			((Button) findViewById(R.id.buttonPineapple))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 8: {
			((Button) findViewById(R.id.buttonLemon))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 9: {
			((Button) findViewById(R.id.buttonGrappe))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 10: {
			((Button) findViewById(R.id.buttonCerise))
					.setVisibility(View.VISIBLE);
		}
			break;
		case 11: {
			((Button) findViewById(R.id.buttonKiwi))
					.setVisibility(View.VISIBLE);
		}
			break;

		}
		;

		((Button) findViewById(R.id.buttonApple))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Pomme";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});

		((Button) findViewById(R.id.buttonBanana))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "banane";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonMelon))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Melon";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonOrange))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Orange";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonPeach))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Peche";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonPear))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Poire";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		// SUITE ICI

		((Button) findViewById(R.id.buttonFraise))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Fraise";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});

		((Button) findViewById(R.id.buttonPineapple))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Ananas";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});

		((Button) findViewById(R.id.buttonLemon))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Citron";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonGrappe))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Raisin";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonCerise))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Cerise";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});
		((Button) findViewById(R.id.buttonKiwi))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Kiwi";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						speakWords(words);
					}
				});

		// check for TTS data
		Intent checkTTSIntent = new Intent();
		checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);

	}

	public void onClick(View v) {

	}

	// speak the user text
	private void speakWords(String speech) {

		// speak straight away
		myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
	}

	// act on result of TTS data check
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == MY_DATA_CHECK_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// the user has the necessary data - create the TTS
				myTTS = new TextToSpeech(this, this);
			} else {
				// no data - install it now
				Intent installTTSIntent = new Intent();
				installTTSIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installTTSIntent);
			}
		}
	}

	// setup TTS
	public void onInit(int initStatus) {

		// check for successful instantiation
		if (initStatus == TextToSpeech.SUCCESS) {
			if (myTTS.isLanguageAvailable(Locale.FRENCH) == TextToSpeech.LANG_AVAILABLE)
				myTTS.setLanguage(Locale.FRENCH);
		} else if (initStatus == TextToSpeech.ERROR) {
			Toast.makeText(this, "Sorry! Text To Speech failed...",
					Toast.LENGTH_LONG).show();
		}
	}
}
